/*
 *  Copyright (c) 2015 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.kedll.library.util;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.kedll.library.KBaseApplication;


/**
 * Toast工具类
 * Created by Jorstin on 2015/3/17.
 */
public class ToastUtil {

    private static Handler handler = new Handler(Looper.getMainLooper());

    private static Toast toast = null;

    private static final Object synObj = new Object();

    private static Snackbar snackbar = null;

    private static Context context = KBaseApplication.getInstance();

    public static void showSnackbar(final View view, final CharSequence msg) {
        showSnackbar(view, msg, Snackbar.LENGTH_LONG);
    }

    public static void showSnackbar(final View view, final CharSequence msg, final int len) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }

        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) {
                    if (snackbar == null) {
                        snackbar = Snackbar.make(view, msg, len);
                    } else {
                        snackbar.setText(msg);
                        snackbar.setDuration(len);
                    }
                    snackbar.show();
                }
            }
        });
    }

    public static void showSnackbar(final View view, final int msg) {
        showSnackbar(view, msg, Snackbar.LENGTH_LONG);
    }

    public static void showSnackbar(final View view, final int msg, final int len) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) {
                    if (snackbar == null) {
                        snackbar = Snackbar.make(view, msg, len);
                    } else {
                        snackbar.setText(msg);
                        snackbar.setDuration(len);
                    }
                    snackbar.show();
                }
            }
        });

    }

    public static void dissmissSnackBar() {
        if (snackbar != null && snackbar.isShown()) {
            snackbar.dismiss();
        }
    }

    public static void showToast(final CharSequence msg) {
        showToast(msg, Toast.LENGTH_SHORT);
    }

    /**
     * 显示一个文本并且设置时长
     *
     * @param msg
     * @param len
     */
    public static void showToast(final CharSequence msg, final int len) {
        if (msg == null || msg.equals("")) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                //加上同步是为了每个toast只要有机会显示出来
                synchronized (synObj) {
                    if (toast != null) {
                        //toast.stop();
                        toast.setText(msg);
                        toast.setDuration(len);
                    } else {
                        toast = Toast.makeText(context, msg, len);
                    }
                    toast.show();
                }
            }
        });
    }

    /**
     * 根据设置的文本显示
     *
     * @param msg
     */
    public static void showToast(final int msg) {
        showToast(msg, Toast.LENGTH_SHORT);
    }

    /**
     * 资源文件方式显示文本
     *
     * @param msg
     * @param len
     */
    public static void showToast(final int msg, final int len) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) {
                    if (toast != null) {
                        //toast.stop();
                        toast.setText(msg);
                        toast.setDuration(len);
                    } else {
                        toast = Toast.makeText(context, msg, len);
                    }
                    toast.show();
                }
            }
        });
    }

    public static void showToastForGravity(final CharSequence msg, final int gravity) {
        if (msg == null || msg.equals("")) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                //加上同步是为了每个toast只要有机会显示出来
                synchronized (synObj) {
                    if (toast != null) {
                        //toast.stop();
                        toast.setText(msg);
                        toast.setDuration(Toast.LENGTH_SHORT);
                    } else {
                        toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                    }
                    toast.setGravity(gravity, 0, 0);
                    toast.show();
                }
            }
        });
    }

    public static void showToast(final View v, final CharSequence msg) {
        ((TextView) v).setText(msg);
    }
}
